//
//  LocationAlertView.m
//  yjw-winner
//
//  Created by 隆霁 on 2018/9/20.
//  Copyright © 2018年 Shanghai decade financial information service co., LTD. All rights reserved.
//

#import "LocationAlertView.h"

@interface LocationAlertView ()<UIPickerViewDelegate, UIPickerViewDataSource> {}

@property (weak, nonatomic) IBOutlet UIButton *cancelButton;
@property (weak, nonatomic) IBOutlet UIButton *makesureButton;
@property (weak, nonatomic) IBOutlet UIPickerView *pickerView;

@property (nonatomic, copy) SettingLocationBlock block;
@property (nonatomic, copy) NSString *location;

@property (nonatomic, strong) NSDictionary *data;
@property (nonatomic, copy) NSString *provinceNumber;
@property (nonatomic, copy) NSString *cityNumber;
@property (nonatomic, copy) NSString *province;
@property (nonatomic, copy) NSString *city;
@property (nonatomic, copy) NSString *area;
@end
@implementation LocationAlertView
- (void)awakeFromNib {
    [super awakeFromNib];
    self.pickerView.delegate = self;
    self.pickerView.dataSource = self;
    
    self.cancelButton.layer.masksToBounds = YES;
    self.cancelButton.layer.cornerRadius = 4.0;
    self.cancelButton.layer.borderColor = [MAIN_COLOR CGColor];
    self.cancelButton.layer.borderWidth = 1.0f;
    
    self.makesureButton.layer.masksToBounds = YES;
    self.makesureButton.layer.cornerRadius = 4.0f;
    self.provinceNumber = [[self.data allKeys] firstObject];
    self.cityNumber = (NSString *)[[[[[self.data valueForKey:self.provinceNumber] allValues] firstObject] allKeys] objectAtIndex:0];
}
- (NSDictionary *)data {
    if (!_data) {
        NSString *path = [[NSBundle mainBundle] pathForResource:@"city" ofType:@"plist"];
        _data = [NSDictionary dictionaryWithContentsOfFile:path];

    }
    return _data;;
}
+ (LocationAlertView *)alert {
    return [[[NSBundle mainBundle] loadNibNamed:NSStringFromClass([LocationAlertView class]) owner:self options:nil] lastObject];
}
/**
 选择地区回调
 
 @param block block
 */
- (void)settingLocationCallBack:(SettingLocationBlock)block {
    self.block = block;
}
#pragma mark --mark UIPickerViewDelegate UIPickerViewDataSource

- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component {
    if (component == 0) {
        NSArray *allProvince = [self.data allKeys];
        self.provinceNumber = allProvince[row];
        self.province = [[[self.data valueForKey:self.provinceNumber] allKeys] firstObject];
        [pickerView reloadComponent:1];
    }else if (component == 1){
        NSDictionary *dic = [self.data valueForKey:self.provinceNumber];
        NSArray *arrValues = [[[dic allValues] firstObject] allValues];
        NSDictionary *d = arrValues[row];
        self.city = [[d allKeys] firstObject];
        NSArray *arrKeys = [[[dic allValues]firstObject] allKeys];
        self.cityNumber = arrKeys[row];
        [pickerView reloadComponent:2];
    }else {
        NSArray *arr = [[[[[[self.data valueForKey:self.provinceNumber] allValues] firstObject] valueForKey:self.cityNumber] allValues] firstObject];
        self.area = arr[row];
    }
    self.location = [NSString stringWithFormat:@"%@ %@ %@", self.province, self.city, self.area];
}
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component {
    if (component == 0) {
        // 省
        return [self.data allKeys].count;
    }else if (component == 1){
        // 市
        NSDictionary *dic = [self.data valueForKey:self.provinceNumber];
        NSArray *arr = [[[dic allValues] firstObject] allValues];
        return arr.count;
    }else {
        // 地区
        NSArray *arr = [[[[[[self.data valueForKey:self.provinceNumber] allValues] firstObject] valueForKey:self.cityNumber] allValues] firstObject];
        return arr.count;
    }
    
}
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView {
    return 3;
}
- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component {
    if (component == 0) {
        NSArray *keys = [self.data allKeys];
        NSString *key = keys[row];
        NSDictionary *dic = [self.data valueForKey:key];
        return [[dic allKeys] firstObject];
    }else if (component == 1){
        NSDictionary *dic = [self.data valueForKey:self.provinceNumber];
        NSArray *arrValues = [[[dic allValues] firstObject] allValues];
        NSDictionary *d = arrValues[row];
        return [[d allKeys] firstObject];
    }else {
        NSArray *arr = [[[[[[self.data valueForKey:self.provinceNumber] allValues] firstObject] valueForKey:self.cityNumber] allValues] firstObject];
        return arr[row];
    }
}



#pragma mark --mark EVENT

- (IBAction)cancelButtonClick:(UIButton *)sender {
    if (self.block) {
        self.block(nil, LocationAlertButtonTypeCancel);
    }
}
- (IBAction)makesureButtonClick:(UIButton *)sender {
    if (self.block && self.location) {
        self.block(self.location, LocationAlertButtonTypeSure);
    }
}

@end
